package com.dai.bookkeeping.frag_record;

import android.inputmethodservice.KeyboardView;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.dai.bookkeeping.R;
import com.dai.bookkeeping.db.AccountBean;
import com.dai.bookkeeping.db.TypeBean;
import com.dai.bookkeeping.utils.BeiZhuDialog;
import com.dai.bookkeeping.utils.KeyBoardUtils;
import com.dai.bookkeeping.utils.SelectTimeDialog;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;


/**
*记录页面当中的支出模块
 */
public abstract class BaseRecordFragment extends Fragment implements View.OnClickListener {
    //初始化键盘
    protected KeyboardView keyboardView;
    //对应键盘数字变化的EditText
    protected EditText moneyEt;
    //选中的图片
    protected ImageView typeIv;
    protected TextView typeTv,beizhuTv,timeTv;
    //九宫格
    protected GridView typeGv;
    protected List<TypeBean> typeList;
    protected TypeBaseAdapter adapter;
    protected AccountBean accountBean;//将需要插入到记账本当中的数据保存成对象形式
    //onCreate是指创建该fragment，类似于Activity.onCreate，你可以在其中初始化除了view之外的东西。
    //onCreateView是创建该fragment对应的视图，你必须在这里创建自己的视图并返回给调用者。

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        accountBean=new AccountBean();//创建对象
        //设置默认值
        accountBean.setTypename("其他");
        accountBean.setsImageId(R.mipmap.ic_qita_fs);

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        //初始化  inflate方法的主要作用就是将xml转换成一个View对象，用于动态的创建布局
//        1.R.layout.item 布局的资源id
//        2.ViewGroup  填充的根视图
//        3.attachToRoot 是否将载入的视图绑定到根视图中
        View view =inflater.inflate(R.layout.fragment_outcome,container,false);
        //初始化view里面的东西
        initView(view);
        setInitTime();
        //给gridview填充数据的方法
        loadDataToGV();
        setGVListener();//设置gridview每一项的点击事件
        return view;
    }
//获取当前时间，显示在timeTV上
    private void setInitTime() {
        TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));
        Date date=new Date();
        //时间转换
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
        String time=sdf.format(date);
        timeTv.setText(time);
        accountBean.setTime(time);

        //得到日历对象
        Calendar calendar=Calendar.getInstance();
        int year=calendar.get(Calendar.YEAR);
        int month=calendar.get(Calendar.MONTH)+1;
        int day=calendar.get(Calendar.DAY_OF_MONTH);
        accountBean.setYear(year);
        accountBean.setMonth(month);
        accountBean.setDay(day);
    }
    //设置gridview每一项的点击事件
    private void setGVListener() {
        typeGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                //图片变化
                adapter.selectPos=position;
                adapter.notifyDataSetChanged();//提示绘制发生变化了
                //设置上面也跟随变化
                TypeBean typeBean=typeList.get(position);
                String typename=typeBean.getTypename();
                //文字变化
                typeTv.setText(typename);
                accountBean.setTypename(typename);
                //图片变化
                int simageId=typeBean.getSimageid();
                System.out.println("--------");
                System.out.println(simageId);
                typeIv.setImageResource(simageId);
                accountBean.setsImageId(simageId);

            }
        });
    }
//加载九宫格
    public void loadDataToGV() {
        typeList=new ArrayList<>();
//        this 是Activity 的实例，扩展了Context，其生命周期是Activity 创建到销毁
//        getApplicationContext() 返回应用的上下文，生命周期是整个应用，应用摧毁它才摧毁
//        Activity.this的context 返回当前activity的上下文，属于activity ，activity 摧毁他就摧毁
        adapter = new TypeBaseAdapter(getContext(), typeList);
        typeGv.setAdapter(adapter);

    }

    private void initView(View view) {
        //键盘
        keyboardView=view.findViewById(R.id.frag_record_keyboard);
        moneyEt=view.findViewById(R.id.frag_record_et_money);
        //图标
        typeIv=view.findViewById(R.id.frag_record_iv);
        //图标旁边的文字
        typeTv=view.findViewById(R.id.frag_record_tv_type);
        //九宫格
        typeGv=view.findViewById(R.id.frag_record_gv);
        beizhuTv=view.findViewById(R.id.frag_record_tv_beizhu);
        timeTv=view.findViewById(R.id.frag_record_tv_time);
        beizhuTv.setOnClickListener(this);
        timeTv.setOnClickListener(this);
        //让自定义软键盘显示出来  传入keyboardView和moneyEt，显示keyboardView键盘与moneyEt随之变化
        KeyBoardUtils boardUtils=new KeyBoardUtils(keyboardView,moneyEt);
        boardUtils.showKeyboard();
        //设置接口，监听确定按钮被点击了
        boardUtils.setOnEnsureListener(new KeyBoardUtils.OnEnsureListener(){
            @Override
            public void onEnsure() {
                //点击确定按钮
                //获取输入钱数
                String moneyStr=moneyEt.getText().toString();
                //判断钱数合法性
                if(TextUtils.isEmpty(moneyStr)||moneyStr.equals("0"))
                {
                    getActivity().finish();
                    return;
                }else{
                    //钱数类型转换为float
                float money=Float.parseFloat(moneyStr);
                accountBean.setMoney(money);

                }
                //获取记录的信息，保存在数据库中
                saveAccountToDB();
                //返回上一级页面
                getActivity().finish();
            }
        });
    }
//让子类一定重写这个方法
    public abstract void saveAccountToDB();
    //点击事件监听
   public void onClick(View v){
       switch (v.getId()) {
           case R.id.frag_record_tv_time:
               showTimeDialog();
               break;
           case R.id.frag_record_tv_beizhu:
               showBZDialog();
               break;
       }
   }
//弹出备注对话框
    private void showBZDialog() {
        final BeiZhuDialog dialog = new BeiZhuDialog(getContext());
        dialog.show();
        dialog.setDialogSize();
        dialog.setOnEnsureListener(new BeiZhuDialog.onEnsureListener() {
            @Override
            public void onEnsure() {
                String msg = dialog.getEditText();
                if (!TextUtils.isEmpty(msg)) {
                    beizhuTv.setText(msg);
                    accountBean.setBeizhu(msg);
                }
                dialog.cancel();
            }
        });
    }
//弹出显示时间的对话框
    public  void showTimeDialog(){
    SelectTimeDialog dialog=new SelectTimeDialog(getContext());
        dialog.show();
        //设定确定按钮被点击了的监听器
        dialog.setOnEnsureListener(new SelectTimeDialog.OnEnsureListener() {
            @Override
            public void onEnsure(String time, int year, int month, int day) {
                timeTv.setText(time);
                accountBean.setTime(time);
                accountBean.setYear(year);
                accountBean.setMonth(month);
                accountBean.setDay(day);
            }
        });
   }
}